import Store from "@/store"
import User from "../model/User";
class LoginService {
    saveUser(data) {

        const map = {
            token: data.token,
            userInfo: new User(data.userInfoDto)
        }

        map.userInfo.administrator = this.getAdministrator(map);
        Store.commit("setPathName", "");
        localStorage.setItem("pathName", "");
        localStorage.setItem("loginInfo", JSON.stringify(map));
        localStorage.setItem("token", data.token);
        Store.commit("SET_TOKEN", data.token);
        Store.commit("SET_USERINFO", map.userInfo);
    }

    getAdministrator(data) {
        let result = false;
        let roles = data.userInfo.roles.filter(val => val.roleId == "1");
        if (roles.length == "1") {
            result = true;
        }

        return result;
    }
}

export default new LoginService();